package com.sesample.seandroidsample;

import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

/**
 * Hosts the data of the products
 * @author phzhou
 *
 */
public class Catalogue {

  private static Catalogue sInstance;

  public static Catalogue getInstance(Context appContext) {
    if (null == sInstance) {
      sInstance = new Catalogue(appContext);
    }
    return sInstance;
  }

  private final Context mAppContext;
  private ArrayList<Product> mProducts = null;

  public Catalogue(Context appContext) {
    mAppContext = appContext;
    try {
      InputStream in = mAppContext.getAssets().open("products.xml");
      CatalogueParser parser = new CatalogueParser();
      mProducts = parser.parse(in);
    }
    catch (Exception e) {
      Log.d("Inventory", "Inventory parsing Exception: " + e.getMessage());
    }
  }

  public Product getProductById(int id) {
    for (Product p : mProducts) {
      if (id == p.getId()) {
        return p;
      }
    }
    return null;
  }

  public ArrayList<Product> getProducts() {
    return mProducts;
  }
}
